//
//  DYTeachingVC.m
//  Yi8Card
//
//  Created by 渴望 on 2019/1/26.
//  Copyright © 2019 diyunkeji. All rights reserved.
//

#import "DYTeachingVC.h"
#import "MyXYCardController.h"
#import <WebKit/WebKit.h>

@interface DYTeachingVC ()<WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;
@end

@implementation DYTeachingVC

- (void)dy_initData{
    [super dy_initData];
    
}

- (void)dy_initUI{
    [super dy_initUI];
    
    self.title = @"智能代还";
    
    
//    UIImage * img = [UIImage imageNamed:@"zhinengdaihuan"];
//    //图片计算高度
//    float imgH = img.size.height*KScreenW/img.size.width;
//
//    UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, imgH)];
//    imgView.image = img;
//
//
//
//    UIScrollView * scroll = [[UIScrollView alloc]init];
//    scroll.showsVerticalScrollIndicator=NO;
//    scroll.contentSize=CGSizeMake(0, imgH);
//    [self.view addSubview:scroll];
//    [scroll mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.right.bottom.equalTo(0);
//    }];
//
//
//    [scroll addSubview:imgView];
    
    
    [self.view addSubview:self.webView];
    
    UIButton * jumpBtn = [[UIButton alloc]init];
    [jumpBtn addTarget:self action:@selector(jumpBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.webView addSubview:jumpBtn];
    [jumpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(0);
        make.height.equalTo(@(200));
    }];
}


- (void)jumpBtnClick{
    MyXYCardController * XYCardC = [[MyXYCardController alloc] init];
    [self.navigationController pushViewController:XYCardC animated:YES];
}




- (void)dy_request{
    [super dy_request];
//    [self.webView loadHTMLString:@"http://ls.baifeidianzi.com/wap/H5web/EasyDetail" baseURL:nil];
    NSURL *url = [NSURL URLWithString:@"http://ls.baifeidianzi.com/wap/H5web/EasyDetail"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
}


- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    XHQHUDSHOW(self.view);
    //    XHQHUDInView(self.view);
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    XHQHUDHIDE(self.view);
    //    XHQHUDHide();
    NSString *title = webView.title;
    if ([NSString xhq_notEmpty:title]) {
        self.navigationItem.title = title;
    }
}

/*
 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
 WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
 NSString *urlString = navigationAction.request.URL.absoluteString;
 urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
 decisionHandler(policy);
 }
 */

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    XHQHUDHIDE(self.view);
    //    XHQHUDInView(self.view);
    //    XHQHUDHide();
}

#pragma mark - getter
- (WKWebView *)webView {
    if (!_webView) {
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
        WKUserContentController *userContent = [[WKUserContentController alloc]init];
        configuration.userContentController = userContent;
        //web 自适应屏幕宽度js
        NSString *jsString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';};";
        WKUserScript *script = [[WKUserScript alloc]initWithSource:jsString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        [userContent addUserScript:script];
        _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationStatusHeight ) configuration:configuration];
        _webView.navigationDelegate = self;
    }
    return _webView;
}
@end
